@extends('backstage.layout.master')
@section('title', '订货单详情')
@section('style')
    <!-- page specific plugin styles -->
    <link rel="stylesheet" href="backstage/assets/css/jquery-ui-1.10.3.custom.min.css"/>
    <link rel="stylesheet" href="backstage/assets/css/chosen.css"/>
    <link rel="stylesheet" href="backstage/assets/css/colorpicker.css"/>
    <!-- fonts -->
    <style>
        .ace-file-input input[type=file] {
            position: static;
        }

        .ace-nav {
            height: 10px;
        }
        .ui-datepicker {
            width: 18em;
        }
        .ui-datepicker{
            width: 15em;
            padding-left: 1em;
        }
        .navbar {
            margin-bottom: 3px;
        }
        #add_temp{
            margin-left: 10px;
            color: #0a03ff;
        }
    </style>
@stop
@section('content')
    <div class="breadcrumbs" id="breadcrumbs">
        <script type="text/javascript">
            try {
                ace.settings.check('breadcrumbs', 'fixed')
            } catch (e) {
            }
        </script>
        <ul class="breadcrumb">
            <li>
                <i class="icon-home home-icon"></i>
                <a target="_blank" href="backstage/home" target="_blank">主页</a>
            </li>
            <li class="active">
                <a target="_blank" href="/backstage/purchase-order/purchase-order-list">订货单列表</a>
            </li>
            <li>
                订货单详情
            </li>
        </ul><!-- .breadcrumb -->
    </div>
    <div class="page-content">
        <div class="row">
            <div class="col-xs-12">
                <!-- PAGE CONTENT BEGINS -->
                <div class="page-header">
                    <h1>
                        订货单列表
                        <small>
                            <i class="icon-double-angle-right"></i>
                            订货单详情
                        </small>
                        <span class="pull-right">
                          <button class="btn btn-primary" onclick="javascript:window.location.href='{{asset('')}}backstage/purchase-order/purchase-order-list';">订货单列表</button>
                         </span>
                    </h1>
                </div>
                <div class="row">
                    <div class="col-xs-12">
                        <!-- PAGE CONTENT BEGINS -->
                        <div class="tabbable">
                            <form id="form" class="form-horizontal">
                                <div class="tab-content no-border padding-24">
                                    <div id="faq-tab-1" class="tab-pane fade in active">
                                        <table id="sample-table-1" class="table table-striped table-bordered table-hover">
                                            <tr>
                                                <td>
                                                    供货商：
                                                </td>
                                                <td colspan="1" >
                                                 <select id="sel_supplier"  class="col-sm-2" style="width: 75%" onchange="selSupplier()">
                                                  <option value=0>请选择....</option>
                                                  @if(isset($suppliers)&&!empty($suppliers))
                                                      @foreach($suppliers as $supplier)
                                                       <option value={{isset($supplier->supplier_id)?$supplier->supplier_id:0}}>{{isset($supplier->supplier_name)?$supplier->supplier_name:''}}</option>
                                                      @endforeach
                                                  @endif
                                                 </select>
                                                </td>
                                                <td>供货商编号：</td>
                                                <td colspan="3">
                                                    <input type="text" readonly name="supplier_id" id="supplier_id">
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>
                                                    实际支付总金额：
                                                </td>
                                                <td >
                                                    <input type="number" step=0.01 min=0 name="actual_fee" value=0 tyle="width:50px;"  class="col-sm-3"/>
                                                </td>
                                                <td></td>
                                                <td></td>
                                                <td></td>
                                                <td></td>
                                            </tr>
                                            <tr>
                                                <td>
                                                    运费：
                                                </td>
                                                <td >
                                                    <input type="number" step=0.01 min=0 name="postage" value=0 tyle="width:50px;"  class="col-sm-3"/>
                                                </td>
                                                <td></td>
                                                <td></td>
                                                <td></td>
                                                <td></td>
                                            </tr>
                                            <tr>
                                                <td>
                                                    快递公司：
                                                </td>
                                                <td colspan="1" >
                                                    <select id="express_id"  name="express_id" class="col-sm-2" style="width: 75%">
                                                        <option value=0>请选择....</option>
                                                        @if(isset($express_list)&&!empty($express_list))
                                                          @foreach($express_list as $express)
                                                           <option value="{{isset($express->express_id)?$express->express_id:''}}">{{isset($express->express_name)?$express->express_name:''}}</option>
                                                          @endforeach
                                                        @endif
                                                    </select>
                                                </td>
                                                <td>快递单号：</td>
                                                <td colspan="3">
                                                    <input type="text"  name="order_express_id" id="order_express_id">
                                                </td>
                                            </tr>
                                            <tr>
                                                <td colspan="6" >备注:
                                                    <textarea  name="remark" style="width: 85%;margin-left: 5%"></textarea>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td colspan="6">可提供模板(品牌|模板名):
                                                    <input  type="button" value="导出Excel" class="button" id="export_excel" style="color: #0a03ff">
                                                </td>
                                            </tr>
                                            <tr id="can_provide_temp">
                                            </tr>
                                            <tr>
                                                <td>
                                                    添加订货：
                                                </td>
                                                <td >
                                                    <input type="text"  id="sku"  style="width: 60%" placeholder="按SKU添加" class="col-sm-3"/>
                                                    <input  type="button" value="添加" class="button" id="add_temp">
                                                </td>
                                                <td></td>
                                                <td></td>
                                                <td></td>
                                                <td></td>
                                            </tr>
                                            <tr>
                                                <td colspan="6" style="color: red;font-weight:bold;">订货明细(一定要点添加！！！)</td>
                                            </tr>
                                            <tr id="purchase_order_detail_list">
                                                <td>sku</td>
                                                <td>品牌|模板名|颜色|尺码</td>
                                                <td>单价</td>
                                                <td>采购数量</td>
                                                <td>支付金额</td>
                                                <td>操作</td>
                                            </tr>
                                        </table>
                                    </div>
                                </div>
                                <div class="clearfix form-actions">
                                    <div class="col-md-offset-5 col-md-9">
                                        <button class="btn btn-info btn-save" type="button">
                                            <i class="icon-ok bigger-110"></i>
                                            保存
                                        </button>
                                    </div>
                                </div>
                            </form>
                            <!-- PAGE CONTENT ENDS -->
                        </div><!-- /.col -->
                    </div><!-- /.row -->
                </div><!-- /.page-content -->
                @stop
@section('script')
<script type="text/javascript">
   _token = '{{csrf_token()}}';
   $(function(){
       var get_sku_state=true;
     $('#add_temp').click(function() {
         var sku=$('#sku').val();
         var supplier_id=parseInt($('#sel_supplier option:selected').val());
         if(!get_sku_state){
             swal('请刷新～','','warning');
             get_sku_state=true;
             return ;
         }
         if(supplier_id==0){
             swal('请选择供货商','','warning');
             get_sku_state=true;
             return ;
         }
         if(sku==''){
             swal({title: "sku为空！", text: "",type: "error", timer:1000 });
         }else {
             get_sku_state=false;
             $.ajax({
                 type: "POST",
                 cache: true,
                 url: "/backstage/purchase-order/get-temp-stock-by-sku",
                 data:{'sku':sku,'supplier_id':supplier_id},
                 success: function (msg) {
                     if(msg.state==1){
                         var  temp_stock=msg.temp_stock;
                         var html='<tr data-tr-id="'+temp_stock.sku+'">';
                         html+='<td class="product_temp_sku">'+temp_stock.sku+'</td>';
                         html+='<td>'+temp_stock.brand_name+'|'+temp_stock.temp_name+'|'+temp_stock.colour_name+'|'+temp_stock.size_name+'</td>';
                         html+='<td>'+
                             '<input type="hidden"  name="product_temp_skus[]" id="temp_sku_'+temp_stock.sku+'" value="'+temp_stock.sku+'" />'
                             +
                             '<input type="number" step=0.01 min=0 name="price" id="sku_price_'+temp_stock.sku+'" value=0 />'
                             +'</td>';
                         html+='<td>'+'' +
                             '<input type="number" step=1 min=0 name="num" id="sku_num_'+temp_stock.sku+'" value=0 />'
                             +'</td>';
                         html+='<td>'+'' +
                             '<input type="number" step=0.01 step=1 min=0 name="actual_fee" id="sku_actual_fee_'+temp_stock.sku+'" value=0 />'
                             +'</td>';
                         html+='<td id="detail_operating_area_'+temp_stock.sku+'">'+
                             '<input type="button"  value="添加" onclick="addProductTemp(&quot;'+temp_stock.sku+'&quot;)" />'
                             +'</td>';
                         html+='</tr>';
                         var flag=true;
                         //遍历SKU是不是已经添加过
                         $("#purchase_order_detail_list").parent().find('.product_temp_sku').each(function(){
                             var sku=$(this).html();
                             if(temp_stock.sku==sku){
                                 flag=false;
                             }
                         });
                         if(flag){
                             $('#sample-table-1').append(html);
                         }
                     }else {
                         swal({title: msg.message, text: "",type: "error", timer:1000 });
                     }
                 }, error: function () {
                     swal({title: "系统繁忙！", text: "",type: "error", timer:1000 })
                 }
             });
         }
         get_sku_state=true;
     });
     var export_excel_state=true;
     $('#export_excel').click(function() {
           var supplier_id=parseInt($('#sel_supplier option:selected').val());
           if(!export_excel_state){
               swal('请刷新～','','warning');
               export_excel_state=true;
               return ;
           }
           if(supplier_id==0){
               swal('请选择供货商','','warning');
               export_excel_state=true;
               return ;
           }
         export_excel_state=false;
         $.ajax({
             type: "POST",
             cache: true,
             url: "/backstage/purchase-order/export-excel",
             data:{'supplier_id':supplier_id},
             success: function (msg) {
                 if(msg.state==1){
                     swal({title: '成功', text: "",type: "success", timer:500 });
                     var xls_url=msg.xls_url;
                     downloadFile(xls_url);
                 }else {
                     swal({title: msg.message, text: "",type: "error", timer:700 });
                 }
             }, error: function () {
                 swal({title: "系统繁忙！", text: "",type: "error", timer:1000 })
             }
         });
         export_excel_state=true;
       });
     var  save_state=1;
       $('.btn-save').click(function() {
           if(save_state==0){
               return false;
           }
           save_state=0;
           $.ajax({
               type: "POST",
               cache: true,
               url: "/backstage/purchase-order/save",
               data:$('#form').serialize(),
               success: function (msg) {
                   if(msg.state==1){
                       swal({title: "保存成功！", text: "",type: "success", timer:500 });
                       window.location.href="/backstage/purchase-order/purchase-order-list";
                   }else {
                       swal({title: msg.message, text: "",type: "error", timer:1000 });
                   }
               }, error: function () {
                   swal({title: "系统繁忙！", text: "",type: "error", timer:1000 });
               }
           });
           save_state=1;
       });
   });
function selSupplier() {
    var supplier_id=parseInt($('#sel_supplier option:selected').val());
    $('#supplier_id').val(supplier_id);
    if(supplier_id!=0){
        $.ajax({
            type: "POST",
            cache: true,
            url: "/backstage/purchase-order/get-temps-by-supplier-id",
            data:{'supplier_id':supplier_id},
            success: function (msg) {
                if(msg.state==1){
                    swal({title: msg.message, text: "",type: "success", timer:500 });
                    var  temps=msg.data;
                    var   html='';
                    for(var a=0;a<temps.length;a++) {
                        var temp=temps[a];
                        html+=temp.brand_name+'|'+temp.name+'('+temp.product_temp_id+')'+'</br>';
                    }
                    $('#can_provide_temp').html(html);
                }else {
                    swal({title: msg.message, text: "",type: "error", timer:1000 });
                }
            }, error: function () {
                swal({title: "系统繁忙！", text: "",type: "error", timer:1000 })
            }
        });
    }

}
function delProductTemp(sku) {
    console.log('sku :'+sku);
    $('[data-tr-id='+sku+']').remove();
}
function addProductTemp(sku) {
    var sku_obj=$('[data-tr-id='+sku+']').find('#temp_sku_'+sku+'');
    var sku_price_obj=$('[data-tr-id='+sku+']').find('#sku_price_'+sku+'');
    var sku_num_obj=$('[data-tr-id='+sku+']').find('#sku_num_'+sku+'');
    var actual_fee_obj=$('[data-tr-id='+sku+']').find('#sku_actual_fee_'+sku+'');
    var sku_val=sku_obj.val();
    var sku_price_val=sku_price_obj.val();
    var sku_num_val=sku_num_obj.val();
    var actual_fee_val=actual_fee_obj.val();
    var detail_operating_area_obj=$('#detail_operating_area_'+sku);
    var str='';
    if((sku_val!='')&&(sku_price_val!='')&&(sku_num_val!='')&&(actual_fee_val!='')){
        str=sku_val+'_'+sku_price_val+'_'+sku_num_val+'_'+actual_fee_val;
    }
    sku_obj.val(str);
    detail_operating_area_obj.html('<input type="button"  value="删除" onclick="delProductTemp(&quot;'+sku+'&quot;)" />');
    sku_price_obj.attr("readonly",true);
    sku_num_obj.attr("readonly",true);
    actual_fee_obj.attr("readonly",true);
}
   /**
    * 下载Excel
    * @param url
    */
   function downloadFile(url){
       if(url==null||url==undefined||url==''){
           console.log('下载地址为空');
           return;
       }
       var iframe = document.createElement("iframe");
       document.body.appendChild(iframe);
       iframe.src =url;
       setTimeout(function(){
           document.body.removeChild(iframe);
       },3000);
   }
</script>
@stop